<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
	
	public	function __construct()
	{
		parent::__construct();
		if($this->session->userdata('user_id') == 12){
		//$this->load->helper("url");
		//$this->load->model("laylich");
		$this->load->model("users");
		//$this->load->model("lich");
		date_default_timezone_set('UTC');
		}
		else{
			redirect('login');
		}
	}
	
	public function index()
	{
		$this->viewAllUser();
	}
	public function viewAllUser()
	{
		$menu['menu'] = "dsnguoidung";
		$data['alluser'] = $this->users->showUser();
		
		$this->load->view('header_block_v1',$menu);
		$this->load->view('content_sidebar_block',$menu);
		$this->load->view("users/viewAllUser",$data);
		$this->load->view('footer_block');
	}
	public function viewEditUser($id_nguoidung)
	{
		$menu["menu"] = "no";		
		$data['nguoidung'] = $this->users->getU($id_nguoidung);
		$quyen = $this->users->getQuyen($id_nguoidung);
		if(count($quyen) > 0){
			foreach($quyen as $q){
				$data['quyen'][] = $q->id_quyen;
			}
		}
		else 
			$data['quyen'] = array();
		$this->load->view('header_block_v1',$menu);
		$this->load->view('content_sidebar_block',$menu);
		$this->load->view("users/viewEditUser",$data);
		$this->load->view('footer_block');
	}
	
	public function viewAddUser()
	{
		$menu["menu"] = "themnguoidung";
		$this->load->view('header_block_v1',$menu);
		$this->load->view('content_sidebar_block',$menu);
		$this->load->view("users/viewAddUser");
		$this->load->view('footer_block');
	}
	
	public function addUser()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		//dieu kien form
		$this->form_validation->set_rules('ten_nguoidung', 'Tên đăng nhập', 'xss_clean|required|min_length[5]|callback_username_check');
		$this->form_validation->set_rules('matkhau_nguoidung', 'Mật khẩu', 'xss_clean|required|min_length[5]|md5');		
		if ($this->form_validation->run() == FALSE){
			$this->viewAddUser();
		}
		else {
			$data["ten_nguoidung"] = $this->input->post("ten_nguoidung");
			$data["matkhau_nguoidung"] = $this->input->post("matkhau_nguoidung");
			$data["tenthat_nguoidung"] = $this->input->post("tenthat_nguoidung");
			$data["chucvu_nguoidung"] = $this->input->post("chucvu_nguoidung");
			$data["phongban_nguoidung"] = $this->input->post("phongban");
					
			$quyen = $this->input->post("quyen");
			$url = base_url()."user";
			if($this->users->insertUser($data,$quyen)){						
				echo "<script>alert('Bạn đã thêm 1 người dùng mới');	window.location='".$url."'; </script>";
			}
			else
				echo "<script>alert('Lỗi: Không insert được!');	window.location='".$url."'; </script>";
		}
	}
	public function username_check($username){		
			$result = $this->users->username_check($username);
			if ($result)
			{
				$this->form_validation->set_message('username_check', '%s đã tồn tại');
				return FALSE;
			}
			else
			{
				return TRUE;
			}
		}
	public function editUser($id_nguoidung)
	{		
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		//dieu kien form
		//$this->form_validation->set_rules('ten_nguoidung', 'Tên đăng nhập', 'xss_clean|required|min_length[5]');
		
		$this->form_validation->set_rules('matkhau_nguoidung', 'Mật khẩu', 'xss_clean|required|min_length[5]|md5');
		if ($this->form_validation->run() == FALSE && $this->input->post("matkhau_nguoidung") != ""){
			$this->viewEditUser($id_nguoidung);
		}
		else {	
		if($this->input->post("matkhau_nguoidung") != "")	
			$data["matkhau_nguoidung"] = $this->input->post("matkhau_nguoidung");
		$data["tenthat_nguoidung"] = $this->input->post("tenthat_nguoidung");
		$data["chucvu_nguoidung"] = $this->input->post("chucvu_nguoidung");
		$data["phongban_nguoidung"] = $this->input->post("phongban");
		
		$quyen = $this->input->post("quyen");
		$url = base_url()."user";
		if($this->users->updateUser($data,$id_nguoidung,$quyen)){			
			echo "<script>alert('Cập nhật thành công');	window.location='".$url."'; </script>";
		}
		else			
			echo "<script>alert('Lỗi: Không update được!');	window.location='".$url."'; </script>";
		}
	}
	public function delUser($id_nguoidung)
	{
		if($this->users->delUser($id_nguoidung)){
			$url = base_url()."user";
			echo "<script>alert('Xóa thành công');	window.location='".$url."'; </script>";
		}
		else{
			echo "Lỗi: Không delete được!";
		}
			
	}
	
}